//
//  DWProfile.swift
//  DWSwift
//
//  Created by GDL on 2025/6/20.
//

import Foundation
import SwiftUI
import DWCoreLib
import SwiftfulRouting

struct DWProfileView: View {
    @Environment(\.router) var router:AnyRouter// 直接访问环境值
    @Binding var userData:String;
    var body: some View {
        GeometryReader { proxy in
           
            VStack(spacing: 0) {
                        // 个人中心内容（非列表）
                Text("用户名：\(userData)")
                            .font(.title)
                DWButton(title: "测试", action: {
                    router.showScreen(.push,onDismiss: {
                       
                    }, destination: { i_router in
                        DWSettingRefresherView().navigationBarHidden(true)
                    })
                }).width(proxy.size.width-40)
            }
            .frame(maxWidth: .infinity,maxHeight: .infinity)
            .background(Color.yellow)
            .onAppear(){
                var tmp:[AnyRoute] = router.getCurrentRouteArray()
            }
        }.modifier(DWScaledLayout())
    }
}

struct DWProfileRefresherView: View{
    @State var isRefreshing = false // 控制刷新状态
    @State var userData = "初始数据" // 个人中心数据
    var body: some View {
        VStack(spacing: 0) {
            CustomNavigationBar(
                title: "个人中心",
                bgColor: .white
            ).statusBar(color: .white)
            DWRefresherView {
                DWProfileView(userData: $userData)
            } refresherAction: {completion in
                Task {
                    // 在后台线程延时 3 秒
                    try? await Task.sleep(nanoseconds: 1 * 1_000_000_000)
                    
                    // 切换到主线程执行 UI 更新
                    await MainActor.run {
                        print("3 秒后执行主线程任务")
                        completion()
                        userData = "新数据数据\(Date())" // UI 操作
                    }
                }

            }
        }
    }
}

struct DWProfileView_Previews: PreviewProvider {
    static var previews: some View {
        DWProfileRefresherView()
    }
}
